"""
    Author:
    Date: 2019-01-22

    礼物管理类
"""

from utils import db


def get_total_income_by_type(income_type):
    """ 获取主播的收益 """
    sql = f'''
        SELECT      IFNULL(SUM(amount), 0) AS amount
        FROM        cmf_users_consumption_log
        WHERE       type={income_type};
    '''
    data = db.query_data_from_db(sql)
    return data[0]['amount'] if data else 0


def get_gift(gift_id):
    """ 根据id获取礼物 """
    sql = f'''
        SELECT      *
        FROM        cmf_config_gift
        WHERE       id={gift_id};
    '''
    data = db.query_data_from_db(sql)
    return data[0] if data else None


def get_gift_list(category_id = None):
    """ 获取礼物列表 """
    where_condition = 'true'
    if category_id is not None:
        where_condition += f' AND g.category_id={category_id}'

    sql = f'''
        SELECT      g.*, c.category_name
        FROM        cmf_config_gift AS g
        JOIN        cmf_config_gift_category AS c ON c.category_id = g.category_id
        WHERE       {where_condition}
        ORDER BY    g.enabled DESC,
                    g.orderno,
                    g.id;
    '''

    data = db.query_data_from_db(sql)
    return data if data else []


def new_gift(name, price, icon, resource, orderno):
    """ 添加新的礼物 """
    sql = '''
        INSERT INTO cmf_config_gift
        SET         name=%(name)s,
                    price=%(price)s,
                    orderno=%(orderno)s,
                    icon=%(icon)s,
                    resource=%(resource)s,
                    enabled=0
    '''
    params = {
        'name': name,
        'price': str(price),
        'icon': icon,
        'resource': resource,
        'orderno': str(orderno),
    }
    affected = db.execute_sql(sql, params)
    return affected == 1


def enable_gift(gift_id):
    """ 启用礼物 """
    sql = f'''
        UPDATE      cmf_config_gift
        SET         enabled=1
        WHERE       id={gift_id} AND
                    enabled=0;
    '''
    affected_rows = db.execute_sql(sql)
    return affected_rows > 0


def disable_gift(gift_id):
    """ 启用礼物 """
    sql = f'''
        UPDATE      cmf_config_gift
        SET         enabled=0
        WHERE       id={gift_id} AND
                    enabled=1;
    '''
    affected_rows = db.execute_sql(sql)
    return affected_rows > 0


def edit_gift(gift_id, name, price, icon, resource, orderno):
    """ 编辑礼物 """
    sql = f'''
        UPDATE      cmf_config_gift
        SET         name=%(name)s,
                    price=%(price)s,
                    orderno=%(orderno)s,
                    icon=%(icon)s,
                    resource=%(resource)s
        WHERE       id=%(gift_id)s;
    '''
    params = {
        'gift_id': str(gift_id),
        'name': name,
        'price': str(price),
        'icon': icon,
        'resource': resource,
        'orderno': str(orderno),
    }
    affected_rows = db.execute_sql(sql, params)
    return affected_rows > 0


def get_gift_category_list():
    """ 获取礼物分类列表 """
    sql = f'''
        SELECT      *
        FROM        cmf_config_gift_category
        ORDER BY    enabled DESC,
                    orderno,
                    id;
    '''
    data = db.query_data_from_db(sql)
    return data if data else []


def new_gift_category(category_id, category_name, orderno):
    """ 添加新的礼物分类 """
    sql = '''
        INSERT INTO cmf_config_gift_category
        SET         category_id=%(category_id)s,
                    category_name=%(category_name)s,
                    orderno=%(orderno)s,
                    enabled=0
    '''
    params = {
        'category_id': category_id,
        'category_name': category_name,
        'orderno': str(orderno),
    }
    affected = db.execute_sql(sql, params)
    return affected == 1


def get_max_category_id():
    """ 获取最大的礼物分类列表 """
    sql = '''
        SELECT    MAX(category_id) AS max_category_id
        FROM      cmf_config_gift_category;
    '''
    data = db.query_data_from_db(sql)
    return data[0]['max_category_id'] if data else 0


def enable_gift_category(gift_category_id):
    """ 启用礼物分类 """
    sql = f'''
        UPDATE      cmf_config_gift_category
        SET         enabled=1
        WHERE       id={gift_category_id} AND
                    enabled=0;
    '''
    affected_rows = db.execute_sql(sql)
    return affected_rows > 0


def disable_gift_category(gift_category_id):
    """ 启用礼物分类 """
    sql = f'''
        UPDATE      cmf_config_gift_category
        SET         enabled=0
        WHERE       id={gift_category_id} AND
                    enabled=1;
    '''
    affected_rows = db.execute_sql(sql)
    return affected_rows > 0


def edit_gift_category(category_id, category_name, orderno):
    """ 编辑礼物分类 """
    sql = f'''
        UPDATE      cmf_config_gift_category
        SET         category_id=%(category_id)s,
                    category_name=%(category_name)s,
                    orderno=%(orderno)s
        WHERE       id=%(category_id)s;
    '''
    params = {
        'category_id': str(category_id),
        'category_name': category_name,
        'orderno': str(orderno),
    }
    affected_rows = db.execute_sql(sql, params)
    return affected_rows > 0


def get_gift_category(category_id):
    """ 根据id获取礼物 """
    sql = f'''
        SELECT      *
        FROM        cmf_config_gift_category
        WHERE       category_id={category_id};
    '''
    data = db.query_data_from_db(sql)
    return data[0] if data else None


def get_gift_count_group_by_category():
    """ 根据礼物分类获取礼物数量 """
    sql = f'''
        SELECT      category_id, COUNT(*) AS gift_count
        FROM        cmf_config_gift
        GROUP BY    category_id;
    '''
    data = db.query_data_from_db(sql)
    if data is None:
        return {}

    gifts_count = {}
    for item in data:
        key = item['category_id']
        value = item['gift_count']
        gifts_count[key] = value
    return gifts_count


if __name__ == '__main__':
    print('ok')
